/**
 * 
 */
package com.shequan.user.service.impl;

import org.apache.log4j.Logger;

import sun.security.provider.MD5;

import com.ailk.dazzle.util.SecurityUtils;
import com.shequan.common.bean.ResultDataStruct;
import com.shequan.common.bean.ViewTextConstans;
import com.shequan.common.bean.WebConstants;
import com.shequan.user.bean.bo.UserInfoBO;
import com.shequan.user.bean.bo.UserSession;
import com.shequan.user.bean.pojo.UserInfo;
import com.shequan.user.dao.UserInfoDAO;
import com.shequan.user.service.UserInfoService;

/**
 * <P>
 * Copyright 2012.
 * </p>
 * <P>
 * All Rights Reserved.
 * </p>
 * 
 * @author YuJF
 * @version 1.0, 2012-2-27
 * @see com.shequan.user.service.impl.UserInfoServiceImpl
 * @since 1.0
 */
public class UserInfoServiceImpl implements UserInfoService {
	private static Logger log = Logger.getLogger(UserInfoServiceImpl.class);
	private UserInfoDAO userInfoDao;

	public void setUserInfoDao(UserInfoDAO userInfoDao) {
		this.userInfoDao = userInfoDao;
	}

	/*
	 * @see
	 * com.shequan.user.service.UserInfoService#loginByUserNameAndPWD(java.lang
	 * .String, java.lang.String)
	 */
	public ResultDataStruct<UserSession> loginByUserNameAndPWD(String userName,
			String userPwd, int loginTimes) {
		log.debug("--- in loginByUserNameAndPWD ---");
		ResultDataStruct<UserSession> rds = new ResultDataStruct<UserSession>();
		log.debug("loginTimes:" + loginTimes);
		if (loginTimes > 2) {
			rds.setBusiSuccess(false);
			rds.setMessage(ViewTextConstans.USER_LOGIN_TIMES_OVER_MSG);
		} else if (userName == null || userName.equals("") || userPwd == null
				|| userPwd.equals("")) {
			rds.setBusiSuccess(false);
			rds.setMessage(ViewTextConstans.USER_NAME_PWD_ISNULL_MSG);
		} else {
			UserInfo userInfo = userInfoDao.selectUserInfoByUsername(userName);
			if (userInfo == null || userInfo.getUserPwd() == null
					|| !userInfo.getUserPwd().equals(userPwd)) {
				rds.setBusiSuccess(false);
				rds.setMessage(ViewTextConstans.USER_NAME_PWD_ISWRONG_MSG);
			} else {
				rds.setBusiSuccess(true);
				rds.setMessage(ViewTextConstans.USER_LOGIN_SUCCESS_MEG);
			}
		}
		return rds;
	}

	/*
	 * @see
	 * com.shequan.user.service.UserInfoService#isEmailReg(java.lang.String)
	 */
	public ResultDataStruct<String> isEmailReg(String userName) {
		ResultDataStruct<String> rds = new ResultDataStruct<String>();
		if (userName == null || userName.equals("")) {
			rds.setBusiSuccess(false);
			rds.setMessage(ViewTextConstans.USER_NAME_REGIST_UNCORRECT_MSG);
		} else {
			UserInfo userInfo = userInfoDao.selectUserInfoByUsername(userName);
			if (userInfo == null) {
				rds.setBusiSuccess(true);
				rds.setMessage(ViewTextConstans.USER_NAME_REGIST_CORRECT_MSG);
			} else {
				rds.setBusiSuccess(false);
				rds.setMessage(ViewTextConstans.USER_NAME_REGIST_UNCORRECT_MSG);
			}
		}
		rds.setSuccess(true);
		return rds;
	}

	/*
	 * @see
	 * com.shequan.user.service.UserInfoService#registBaseInfo(com.shequan.user
	 * .bean.bo.UserInfoBO)
	 */
	public ResultDataStruct<String> registBaseInfo(UserInfoBO userInfoBO) {
		ResultDataStruct<String> rds = new ResultDataStruct<String>();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userInfoBO.getUserName());
		userInfo.setRealName(userInfoBO.getRealName());
		userInfo.setUserPwd(userInfoBO.getUserPwd());
		userInfo.setUserBirthday(userInfoBO.getBirthday());
		userInfo.setUserGender(Integer.parseInt(userInfoBO.getGender()));
		int row = userInfoDao.insert(userInfo);
		if (row == 1) {
			rds.setBusiSuccess(true);
			log.debug("新插入的用户ID："+userInfo.getUserId());
			rds.setData(String.valueOf(userInfo.getUserId()));
			rds.setMessage(ViewTextConstans.USER_REGIST_SUCCESS_MEG);
		} else {
			rds.setMessage(ViewTextConstans.USER_REGIST_UNSUCCESS_MEG);
		}
		rds.setSuccess(true);
		return rds;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.shequan.user.service.UserInfoService#activityUserByKey(java.lang.
	 * String, java.lang.String)
	 */
	public ResultDataStruct<UserInfo> activityUserByKey(String userName,
			String encryptValue) {
		ResultDataStruct<UserInfo> rds = new ResultDataStruct<UserInfo>();
		UserInfo userInfo = userInfoDao.selectUserInfoByUsername(userName);
		try {
			String userNamePsw = SecurityUtils.decryptByDES(encryptValue,
					WebConstants.ENCRYPT_KEY);
			String[] userNameAndPsw = userNamePsw
					.split(WebConstants.ENCRYPT_SEPARATIVE);
			if (userNameAndPsw[0].equals(userName)
					&& userNameAndPsw[1].equals(userInfo.getUserPwd())) {
				userInfo.setAccountStatus(1);// 账号状态 0-未激活 1-已激活 3-已冻结 4-已注销
				userInfoDao.updateByPrimaryKey(userInfo);
				rds.setBusiSuccess(true);
				rds.setData(userInfo);
				rds.setMessage(ViewTextConstans.USER_ACTIVITY_SUCCESS);
			} else {
				rds.setBusiSuccess(false);
				rds.setMessage(ViewTextConstans.USER_ACTIVITY_FAILSE);
			}
		} catch (Exception e) {
			rds.setSuccess(false);
			rds.setMessage(ViewTextConstans.USER_ACTIVITY_FAILSE);
		}
		return rds;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.shequan.user.service.UserInfoService#modifyUserInfo(com.shequan.user
	 * .bean.bo.UserInfoBO)
	 */
	public ResultDataStruct<String> modifyUserInfo(UserInfoBO userInfoBo) {
		ResultDataStruct<String> rts = new ResultDataStruct<String>();
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userInfoBo.getUserName());
		userInfo.setUserPhoto(userInfoBo.getTel());
		int rowNumber = userInfoDao.updateByUserNameSelective(userInfo);
		if (rowNumber == 1) {
			rts.setBusiSuccess(true);
			log.debug("被修改用户ID：" + userInfo.getUserId());
			rts.setData(String.valueOf(userInfo.getUserId()));
		} else {
			rts.setMessage(ViewTextConstans.USER_REGIST_UNSUCCESS_MEG);
		}
		return rts;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.shequan.user.service.UserInfoService#queryUserInfoByUserName(java
	 * .lang.String)
	 */
	public UserInfo queryUserInfoByUserName(String userName) {
		UserInfo userInfo = userInfoDao.selectUserInfoByUsername(userName);
		return userInfo;
	}

	public UserInfo queryUserByUserNameAndPWD(String userName, String password) {
		UserInfo userInfo = new UserInfo();
		userInfo.setUserName(userName);
		userInfo.setUserPwd(password);
		return userInfoDao.queryUserByUserNameAndPWD(userInfo);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.shequan.user.service.UserInfoService#isCorrectPwd(java.lang.String,
	 * java.lang.String)
	 */
	public ResultDataStruct<String> isCorrectPwd(String userPwd,
			String userPwdConfirm) {
		ResultDataStruct<String> rds = new ResultDataStruct<String>();

		if (userPwd == null || userPwdConfirm == null) {
			rds.setBusiSuccess(false);
			rds.setMessage(ViewTextConstans.USER_REGIST_PWD_UNCORRECT);
		} else if (!userPwd.equals(userPwdConfirm)) {
			rds.setBusiSuccess(false);
			rds.setMessage(ViewTextConstans.USER_REGIST_PWD_UNCORRECT);
		} else {
			rds.setBusiSuccess(true);
			rds.setMessage(ViewTextConstans.USER_REGIST_SUCCESS_MEG);
		}
		rds.setSuccess(true);
		return rds;
	}

}
